// pages/one_group/one_group.js
Page({

  /**
   * Page initial data
   */
  data: {
    num: 0,
    _id: 0,
    name: "",
    value: "",
    fgColor: 'black',
    btn: 0
  },
  previewImage() {
    // 在自定义组件下，当前组件实例的 this，以操作组件内 <canvas> 组件
    const that = this.selectComponent('#qrcode')

    wx.canvasToTempFilePath({
      canvasId: 'wux-qrcode',
      success: (res) => {
        wx.previewImage({
          urls: [res.tempFilePath]
        })
      }
    }, that)
  },

  /**
   * Lifecycle function--Called when page load
   */
  onLoad: function(options) {
    var that = this

    that.setData({
      num: parseInt(options.num),
      _id: options._id,
      name: options.name,
      value: options._id + "," + options.name,
    })

  },

  handleClick: function(e) {
    var that = this
    if (that.data.num == 1) {
      wx.showModal({
        title: '确认！',
        content: '您真的要退出该群组吗？',
        success(res) {
          if (res.confirm) {
            wx.showLoading({
              title: '退出中',
            })
            that.setData({
              btn: 1
            })
            wx.login({
              success(resout) {
                if (resout.code) {
                  // 退出群组
                  wx.request({
                    url: 'https://www.ourspark.org/helloworld/deleteGroup',
                    data: {
                      _id: that.data._id,
                      name: that.data.name,
                      num: 1,
                      code: resout.code
                    },
                    method: "POST",
                    header: {
                      "Content-Type": "application/x-www-form-urlencoded"
                    },
                    success: function (res) {
                      console.log(res)
                      if (res.data.error_no == 2) {
                        wx.showModal({
                          title: "哎呀~",
                          content: res.data.error_msg,
                        })
                      } else if (res.data.error_no == 1) {
                        wx.hideLoading()
                        that.setData({
                          btn: 0
                        })

                        wx.navigateBack({
                          delta: 1
                        })
                      }
                    },
                    fail: function (res) {

                    }
                  })
                } else {
                  console.log('登录失败！' + res.errMsg)
                }
              }
            })
            
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else if (that.data.num == 2) {
      wx.showModal({
        title: '确认！',
        content: '您真的要解散该群组吗？',
        success(res) {
          if (res.confirm) {
            wx.showLoading({
              title: '删除中',
            })
            that.setData({
              btn: 1
            })
            // 解散群组
            wx.request({
              url: 'https://www.ourspark.org/helloworld/deleteGroup',
              data: {
                _id: that.data._id,
                name: that.data.name,
                num: 2
              },
              method: "POST",
              header: {
                "Content-Type": "application/x-www-form-urlencoded"
              },
              success: function(res) {
                console.log(res)
                if (res.data.error_no == 2) {
                  wx.showModal({
                    title: "哎呀~",
                    content: res.data.error_msg,
                  })
                } else if (res.data.error_no == 1) {
                  wx.hideLoading()
                  that.setData({
                    btn: 0
                  })

                  wx.navigateBack({
                    delta: 1
                  })
                }
              },
              fail: function(res) {

              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })

    }
  },

  /**
   * Lifecycle function--Called when page is initially rendered
   */
  onReady: function() {

  },

  /**
   * Lifecycle function--Called when page show
   */
  onShow: function() {

  },

  /**
   * Lifecycle function--Called when page hide
   */
  onHide: function() {

  },

  /**
   * Lifecycle function--Called when page unload
   */
  onUnload: function() {

  },

  /**
   * Page event handler function--Called when user drop down
   */
  onPullDownRefresh: function() {

  },

  /**
   * Called when page reach bottom
   */
  onReachBottom: function() {

  },

  /**
   * Called when user click on the top right corner to share
   */
  onShareAppMessage: function() {

  }
})